#include '../../AiScriptsRefs/doEx.jsx'
// var docRef = app.activeDocument;
// var textFrameRef = _getCurrTextFrame();

var style = {
  en: {
    fontIndex: _getFontIndex('PFDINTextCondPro-Medium'),
    scale: 1.1,
    tracking: 80,
    baselineShift: 0.04,
  },
  zh: {
    fontIndex: _getFontIndex('HYQiHeiX3-GES'),
    tracking: 120,
    leading: 1.5,
  }
}

var content = '捷达VS5'

function setInsideCharactersAttr(targetTextFrame, source, attr) {
  var characters = targetTextFrame.textRange.characters
  var targetStr = ''
  for (var i = 0, len = characters.length; i < len; i++) {
    targetStr += characters[i].contents
  }
  var ranges = getStrPositions(targetStr, source)
  for (var i = 0, len = ranges.length; i < len; i++) {
    for (var index = ranges[i][0]; index <= ranges[i][1]; index++) {
      var attr = __isInCharSet(characters[index].contents) ? style.en : style.zh
      setCharacterAttr(characters[index], attr)
    }
  }
}

doEx('selection').filter('textFrames').each(function (textFrame) {
  var characters = textFrame.textRange.characters
  var targetStr = ''
  for (var i = 0, len = characters.length; i < len; i++) {
    targetStr += characters[i].contents
  }
  var ranges = getStrPositions(targetStr, content)
  for (var i = 0, len = ranges.length; i < len; i++) {
    for (var index = ranges[i][0]; index <= ranges[i][1]; index++) {
      var attr = __isInCharSet(characters[index].contents) ? style.en : style.zh
      setCharacterAttr(characters[index], attr)
    }
  }
})


function getStrPositions(target, source) {
  var segments = target.split(source)
  var startIndex = 0
  var ranges = []
  for (var i = 0, len = segments.length; i < len - 1; i++) {
    startIndex += segments[i].length
    ranges.push([startIndex, startIndex + source.length - 1])
    startIndex += source.length
  }
  return ranges
}

function setCharacterAttr(character, attr) {
  var characterAttributes = character.characterAttributes
  if (attr.size) { characterAttributes.size = attr.size }
  var size = characterAttributes.size
  characterAttributes.textFont = app.textFonts[attr.fontIndex];
  characterAttributes.horizontalScale = (attr.scale || 1) * 100;
  characterAttributes.verticalScale = (attr.scale || 1) * 100;
  if (attr.tracking) {
    characterAttributes.tracking = attr.tracking;
  }
  characterAttributes.baselineShift = -size * (attr.baselineShift || 0);
  // characterAttributes.autoLeading = false;
  // characterAttributes.leading = size * 1.5;
}


/*
if (textFrameRef) {
  setJettaTitle(textFrameRef)
} else {
  alert('请选择文本')
}
*/

function setJettaTitle(textFrameRef) {
  _setParagraphAttr(textFrameRef, {
    fontIndex: _getFontIndex('HYQiHeiX3-GES'),
    tracking: 120,
    leading: 1.5,
  })
  _setCharacterAttr(textFrameRef, {
    fontIndex: _getFontIndex('PFDINTextCondPro-Medium'),
    scale: 1.1,
    tracking: 80,
    baselineShift: 0.04,
  })
}

function _setParagraphAttr(textFrameRef, attr){
  var textRange = textFrameRef.textRange
  textRange.textFont = app.textFonts[attr.fontIndex];
  textRange.tracking = attr.tracking;
  textRange.autoLeading = false;
  textRange.leading = (attr.size || textRange.size) * attr.leading;
  if(attr.contents) {
    textRange.contents = attr.contents;
  }
  if(attr.top) {
    textRange.top = attr.top;
  }
  if(attr.left) {
    textRange.left = attr.left;
  }
  if(attr.size) { textRange.size = attr.size; }
}

function _setCharacterAttr(textFrameRef, attr) {
  var charCount = textFrameRef.textRange.characters.length;
  for(i=0; i<charCount; i++) {
      var charRef = textFrameRef.textRange.characters[i].contents
      var characterAttributes = textFrameRef.textRange.characters[i].characterAttributes
      var size = characterAttributes.size
      if (__isInCharSet(charRef)) {
          characterAttributes.textFont = app.textFonts[attr.fontIndex];
          characterAttributes.horizontalScale = attr.scale * 100;
          characterAttributes.verticalScale = attr.scale * 100;
          if (attr.tracking) {
            characterAttributes.tracking = attr.tracking;
          }
          characterAttributes.baselineShift = -size * attr.baselineShift;
          // characterAttributes.autoLeading = false;
          // characterAttributes.leading = size * 1.5;
      }
  }
}

function _getCurrTextFrame() {
    var selectedItems = app.activeDocument.selection;
    if ( selectedItems.length > 0 ) {
      if ( selectedItems[0].typename == "TextFrame" ) {
        return selectedItems[0]
      }
    }
        return selectedItems[0]
    return null
  }

  function __isInCharSet(character) {
    var charSet = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-';
    return charSet.indexOf(character) != -1;
  }

  function _getFontIndex(name) {
    var index = 0
    for(var i = 0; i < app.textFonts.length; i++) {
        if(app.textFonts[i].name === name) {
            index = i
            break;
        }
    }
    return index
  }
